﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Tebbee.UI.Client.AccountSer;
using Tebbee.UI.Client.UtilitiesSer;

namespace Tebbee.UI.Client.Controllers
{
    public class AdminPublisherController : ApiController
    {
        private readonly AccountSerClient db = new AccountSerClient();
        private readonly UtilitiesClient utilities = new UtilitiesClient();

        public class PostPublisherRequest
        {
            public PublisherFull pub { get; set; }
            public int packagePub { get; set; }
        }

        public class RegPublisherRequest
        {
            public int pubId { get; set; }
            public int packagePub { get; set; }
        }

        public HttpResponseMessage ResetPassword(string id)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (db.ResetPassword(id) != 0)
            {
                return Request.CreateResponse(HttpStatusCode.OK, id);
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Not Found");
            }
        }

        public HttpResponseMessage RegPackage(RegPublisherRequest request)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            int pubId = request.pubId;
            DateTime availableDate = DateTime.Now;
            DateTime expriedDate = DateTime.Now;

            int packagePub = request.packagePub;
            switch (packagePub)
            {
                case 1:
                    expriedDate = DateTime.Now.AddMonths(1);
                    break;
                case 2:
                    expriedDate = DateTime.Now.AddMonths(3);
                    break;
                case 3:
                    expriedDate = DateTime.Now.AddYears(1);
                    break;
                default:
                    packagePub = 0;
                    break;
            }

            if (packagePub != 0 && db.PublisherRegPackage(pubId, availableDate, expriedDate) != 0)
            {
                return Request.CreateResponse(HttpStatusCode.OK, pubId);
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Not Found");
            }
        } 

        public HttpResponseMessage AddNewPublisher(PostPublisherRequest request)
        {
            if (ModelState.IsValid)
            {
                PublisherFull pub = request.pub;
                pub.AvailableDate = DateTime.Now;

                int packagePub = request.packagePub;
                switch (packagePub){
                    case 1:
                        pub.ExpriedDate = DateTime.Now.AddMonths(1);
		                break;
	                case 2:
                        pub.ExpriedDate = DateTime.Now.AddMonths(3);
		                break;
                    case 3:
                        pub.ExpriedDate = DateTime.Now.AddYears(1);
                        break;
                    default:
                        packagePub = 0;
                        break;
                }

                string msg = "";
                if (packagePub != 0 && db.AddNewPublisher(out msg, pub) != 0)
                {
                    var fileName = "Email_DangKyThanhCong.html";
                    var subjecfile = "Subject_DangKyThanhCong.html";
                    var filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Advertise/Email/" + fileName);
                    var subjectPath = Path.Combine(HttpRuntime.AppDomainAppPath, "Advertise/Email/" + subjecfile);
                    try
                    {
                        var Body = utilities.ReadFile(filePath);
                        Body = Body.Replace("#tb{UserName}", pub.UserName);
                        var Subject = utilities.ReadFile(subjectPath);
                        utilities.SendMail(pub.Email, Body, Subject);
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.Message);
                    }
                    return Request.CreateResponse(HttpStatusCode.Created, pub);
                }
                else
                {
                    HttpError err = new HttpError();
                    err.Message = msg;
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, err);
                }
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        public HttpResponseMessage EditPublisher(PostPublisherRequest request)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            PublisherFull pub = request.pub;
            int packagePub = request.packagePub;

            if (pub.ExpriedDate < DateTime.Now && packagePub != 0)
            {
                pub.ExpriedDate = DateTime.Now;
            }

            switch (packagePub)
            {
                case 1:
                    pub.ExpriedDate = pub.ExpriedDate.AddMonths(1);
                    break;
                case 2:
                    pub.ExpriedDate = pub.ExpriedDate.AddMonths(3);
                    break;
                case 3:
                    pub.ExpriedDate = pub.ExpriedDate.AddYears(1);
                    break;
                default: break;
            }

            string msg = "";
            if (packagePub != 0 && db.EditProfilesPublisher(out msg, pub) != 0)
            {
                return Request.CreateResponse(HttpStatusCode.OK, pub);
            }
            else
            {
                HttpError err = new HttpError();
                err.Message = msg;
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, err);
            }
        }

        public HttpResponseMessage DeletePublisher(int id)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (db.DeletePublisher(id) != 0)
            {
                return Request.CreateResponse(HttpStatusCode.OK, id);
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Not Found");
            }
        }

        public IEnumerable<Publisher_List_Result> GetPublishers(int id)
        {
            var queryString = HttpUtility.ParseQueryString(Request.RequestUri.Query);
            if (id < 1) id = 1;
            id = (id - 1) * 10;
            var tmp = db.ListPublishers(id, queryString["value"]);
            return tmp;
        }

        public IEnumerable<Publisher_All_Result> GetAllPublishers()
        {
            return db.AllPublishers();
        }
    }
}
